package values.item
{
import values.dao_ns;
import values.events.ItemValueEvent;

[Event(name="numChanged", type="values.events.ItemValueEvent")]
/**
 * NumbericItemValue
 * @author jliang
 *
 */
public class NumbericItemValue extends ItemValue implements INumbericItemValue
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function NumbericItemValue()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get numbericTemplate():NumbericItemTemplateValue
	{
		return NumbericItemTemplateValue(template);
	}
	//==========================================================================
	//  Overridden methods: ItemValue
	//==========================================================================
	override public function initialize(params:*):void
	{		
		super.initialize(params);
		
		if (!Object(params).hasOwnProperty("num"))
		{
			params.num = 0;
		}
	}
	
	override public function toObject():Object
	{
        var obj:Object = super.toObject(); 
        obj.push({
            num: num
        });
        return obj;
	}
	//==========================================================================
	//  interface - INumbericItemValue
	//==========================================================================
	public function get num():Number
	{
		return params.num;
	}

	public function set num(value:Number):void
	{
		params.num = value;
	}
	
	public function get groupCount():Number
	{
		return numbericTemplate.groupCount;
	}
	
	public function get isFull():Boolean
	{
		return num >= groupCount;
	}
	
	public function concat(value:INumbericItemValue):void
	{
		var oldNum:Number = num;		
		num += value.num;
		
		var evt:ItemValueEvent = new ItemValueEvent(ItemValueEvent.NUM_CHANGED);
		evt.params = { oldValue:oldNum, newValue:num, value:this };
		this.dispatchEvent(evt);
	}
	
	public function forceUpdate():void
	{
		var evt:ItemValueEvent = new ItemValueEvent(ItemValueEvent.NUM_CHANGED);
		evt.params = { oldValue:num, newValue:num, value:this };
		this.dispatchEvent(evt);
	}
	
	public function clone():INumbericItemValue
	{
		var vo:NumbericItemValue = new NumbericItemValue();
		vo.dao_ns::template = template;
		vo.initialize(params);
		vo.num = num;
		return vo;
	}
	
	override public function toString():String
	{
		return num + " " + name;
	}
}
}



